package com.hy.frame.base;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;


/**
 * title SuperActivity
 * author HeYan
 * time 2021/1/26 17:47
 * desc 不要写与具体项目相关的代码
 */
public abstract class SuperFragment extends Fragment {

    @Nullable
    public <T extends View> T findViewById(@IdRes int id) {
        if (getView() == null) return null;
        return getView().findViewById(id);
    }

    public <T extends View> T findViewById(int id, View parent) {
        if (parent != null) return parent.findViewById(id);
        return findViewById(id);
    }

    /**
     * 获取根布局
     */
    @Nullable
    protected abstract View getRootLayout(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState);

    /**
     * 初始化控件
     */
    protected abstract void initView();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return getRootLayout(inflater, container, savedInstanceState);
    }

    @Override
    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        initView();
        initData();
    }
}
